package org.yxj.breakpointdownloadingapp;

import android.os.Environment;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;

import com.google.gson.Gson;
import com.jbh.permissionlibrary.base.BasePermissionActivity;

import org.yxj.lib_downfile.LogUtil;
import org.yxj.lib_downfile.bean.DownFileBean;
import org.yxj.lib_downfile.i.DownListener;
import org.yxj.lib_downfile.i.ResultListener;
import org.yxj.lib_downfile.m.AppSQLiteManager;
import org.yxj.lib_downfile.m.DownFileManager;

import java.io.File;

public class MainActivity extends BasePermissionActivity implements View.OnClickListener {


    /**
     * sdcard 跟目录
     */
    private String path = Environment.getExternalStorageDirectory().getAbsolutePath() +
            File.separator + "zj.apk";


    private ProgressBar progressBar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar = findViewById(R.id.progress);
        findViewById(R.id.btn_startDown).setOnClickListener(this::onClick);
        findViewById(R.id.btn_pause).setOnClickListener(this::onClick);
        DownFileManager.getInstance().defaultProgress(url, new ResultListener<Long>() {
            @Override
            public void onResult(Long aLong) {
                listener.onProgress(aLong);
            }
        });
    }


    private String url = "https://i.zhaojinapp.com/Common/APK/2308/02/2308021038039073.apk";


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //开始下载
            case R.id.btn_startDown:
                /**
                 * 检测是否已经下载
                 */
                DownFileManager.getInstance().isFileDowned(path, url, new ResultListener<Boolean>() {
                    @Override
                    public void onResult(Boolean aBoolean) {
                        if (aBoolean) {
                            //已经下载
                            LogUtil.LOG_V("[isFileDowned]->文件已经存在");
                            new File(path).delete();
                            AppSQLiteManager.getInstance().getDownFileDao().clear();
                        }

                        //开始下载
                        DownFileManager.getInstance()
                                .listener(listener)
                                .downUrl(url)
                                .saveFilePath(path)
                                .start();
                    }
                });
                break;

            /**
             * 暂停或开始
             */
            case R.id.btn_pause:
                DownFileManager.getInstance().stop();
                break;
        }
    }


    private DownListener listener = new DownListener() {

        @Override
        public void onPrepare(String msg, long progress, long total) {
            LogUtil.LOG_V("[DownListener]->msg = " + msg);
        }

        @Override
        public void onProgress(long progress) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    LogUtil.LOG_V("[DownListener]->progress = " + progress);
                    progressBar.setProgress((int) progress);
                }
            });
        }

        @Override
        public void onComplete(DownFileBean downFileBean) {
            LogUtil.LOG_V("[DownListener]->onComplete = " + new Gson().toJson(downFileBean));
        }

        @Override
        public void onFail(String error) {
            LogUtil.LOG_V("[DownListener]->onFail = " + error);
        }
    };
}